
package com.cn.mobileproject.view.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.cn.mobileproject.R;
import com.cn.mobileproject.bean.User;

public class UserAdapter extends BaseAdapter {
    public interface IOnOper{
        public void onDelete();
    }
    private List<User> mList;

    private Context mContext;
    
    private IOnOper mOper = null;

    public UserAdapter(Context context, IOnOper oper) {
        super();
        mOper = oper;
        mList = new ArrayList<User>();
        mContext = context;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    public void setData(List<User> list) {
        mList = list;
    }

    public void addItem(User userInfo) {
        mList.add(userInfo);
    }

    public void removeItem(int idx) {
        mList.remove(idx);
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        User userInfo = mList.get(position);
        Holder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.nd_user_item, null);
            holder = new Holder();
            holder.tvUsername = (TextView)convertView.findViewById(R.id.tvUsername);
            holder.btnDel = (ImageButton)convertView.findViewById(R.id.btnDel);
            holder.btnDel.setVisibility(View.GONE);
            convertView.setTag(holder);
        } else
            holder = (Holder)convertView.getTag();

        holder.btnDel.setTag(position);
        holder.btnDel.setFocusable(false);
        holder.tvUsername.setText(userInfo.account);


        return convertView;
    }

    private class Holder {
        ImageButton btnDel;

        TextView tvUsername;
    }
   
}
